package com.spzx.product.controller;


import com.spzx.common.core.web.controller.BaseController;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.web.page.TableDataInfo;
import com.spzx.common.security.annotation.InnerAuth;
import com.spzx.common.security.annotation.RequiresPermissions;
import com.spzx.common.security.utils.SecurityUtils;
import com.spzx.product.api.domain.Brand;
import com.spzx.product.service.IBrandService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "品牌接口管理")
@RestController
@RequestMapping("/brand")
public class BrandController extends BaseController
{


    @Autowired
    private IBrandService brandService;

    /**
     * 查询品牌列表
     */
    @Operation(summary = "查询品牌列表")
    @GetMapping("/list")
    public TableDataInfo list(Brand brand) {

        startPage();
        List<Brand> list = brandService.selectBrandList(brand);
        TableDataInfo dataTable = getDataTable(list);
        return dataTable;

    }

    @Operation(summary = "获取品牌详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        Brand brand = brandService.selectBrandById(id);
        return success(brand);
    }

    @Operation(summary = "新增品牌")
    @PostMapping
   // public AjaxResult add(@RequestBody  @Validated  Brand brand) {
    public AjaxResult add(@RequestBody   Brand brand) {

        brand.setCreateBy(SecurityUtils.getUsername());
        int i = brandService.insertBrand(brand);
        return toAjax(i);
    }

    @Operation(summary = "删除品牌")
    @DeleteMapping("/{ids}")
    @RequiresPermissions(value = "product:brand:remove")
    public AjaxResult remove(@PathVariable Long[] ids) {

        boolean b = brandService.removeByIds(List.of(ids));
        return  toAjax(b);
       // return toAjax(brandService.deleteBrandByIds(ids));

    }

    @InnerAuth
    @Operation(summary = "获取全部品牌")
    @GetMapping("/getBrandAll")
    public AjaxResult getBrandAll() {

        List<Brand> brands = brandService.selectBrandList(null);
        return success(brands);
    }

    @Operation(summary = "修改品牌")
    @PutMapping
    public AjaxResult edit(@RequestBody Brand brand) {
        brand.setUpdateBy(SecurityUtils.getUsername());
        boolean b = brandService.updateById(brand);
        return toAjax(b);
    }

}